<?php

/**
 * This is the model class for table "{{seolinks}}".
 *
 * The followings are the available columns in table '{{seolinks}}':
 * @property string $linkId
 * @property string $linkType
 * @property string $linkActive
 * @property string $linkLastShowed
 * @property string $linkContent
 * @property string $linkPublishCount
 */
class Seolinks extends CActiveRecord {

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{seolinks}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('linkType', 'length', 'max' => 255),
			array('linkContent', 'length', 'max' => 1024),
			array('linkActive, linkLastShowed, linkPublishCount', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('linkId, linkType, linkActive, linkLastShowed, linkContent, linkPublishCount, linkModelCount', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'linkId' => 'ID',
			'linkType' => 'Позиция',
			'linkActive' => 'Активный',
			'linkLastShowed' => 'Последний показ',
			'linkContent' => 'Cодержимое',
			'linkModelCount' => 'Количество публикаций',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search() {
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('"linkId"', $this->linkId);
		$criteria->compare('"linkType"', $this->linkType);
		$criteria->compare('"linkActive"', $this->linkActive);
		$criteria->compare('"linkLastShowed"', $this->linkLastShowed, true);
		$criteria->compare('"linkContent"', $this->linkContent, true);
		$criteria->compare('"linkPublishCount"', $this->linkPublishCount);
		$criteria->compare('"linkModelCount"', $this->linkModelCount);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

	public static function getUrl($ids) {
		$st = '';
		$ids = substr($ids, 1, strlen($ids));
		$ids = substr($ids, 0, strlen($ids) - 1);
		$arr = explode('~~', $ids);
		$c = new CDbCriteria();
		$c->select = '"pageUrl"';
		$c->addInCondition('"pageId"', $arr);
		$models = Page::model()->findAll($c);
		foreach ($models as $model) {
			$st .= '<a target="_blank" href="http://mobi03.ru/' . $model['pageUrl'] . '">http://mobi03.ru/' . $model['pageUrl'] . '</a><br />';
		}
		return $st;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Seolinks the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

}
